package com.qjc.demo.observer.controller;

import com.qjc.demo.observer.event.MessageVO;
import com.qjc.demo.observer.event.ObserverEvent;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 文件描述
 *
 * @ClassName: TestObserverController
 * @Author: qjc
 * @Date: 2023/5/10 15:44
 */
@RestController
@RequestMapping("/observer")
public class TestObserverController {

    @Resource
    private ApplicationContext applicationContext;

    /**
     * http://localhost:8081/observer/test?sendTo=111&content=消息内容
     *
     * @param sendTo：消息接收者
     * @param content：消息内容
     * @return
     */
    @GetMapping("/test")
    public String testObserver(@RequestParam(name = "sendTo") String sendTo, @RequestParam(name = "content") String content) {
        MessageVO messageVO = new MessageVO();
        messageVO.setSendTo(sendTo);
        messageVO.setContent(content);
        applicationContext.publishEvent(ObserverEvent.buildEvent(messageVO));
        return "发送成功，请查看控制台输出内容";
    }

}
